<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/order/manager');
	}
	
	//Quan ly don dat hang cho kiem duyet
	public function waitcensor(){
		$this->headTitle('Đơn đặt hàng chờ kiểm duyệt - Admin Panel');
		$this->loadData('action',array('name' => 'waitcensor', 'display_name' => 'Chờ kiểm duyệt'));
		$act = $this->input->get('act', true);
		if(!empty($act)){
			if($act == "view"){
				$orderid = $this->input->get('orderid', true);
				$this->detail(0,'waitcensor');
				if($this->input->post('btnAcceptDDH')){
					$this->load->model('Orders_Model');
					$this->Orders_Model->updateAcceptOrder($orderid);
					redirect('administrator/order/waitcensor');
				}
				if($this->input->post('btnCancelDDH')){
					$this->cancel($orderid, 'waitcensor');	
				}
			}
		}else{
			$this->manager(0,'waitcensor');
		}
	}
	
	//Quan ly don dat hang dang hoat dong
	public function isactive(){
		$this->headTitle('Đơn đặt hàng đang hoạt động - Admin Panel');
		$this->loadData('action',array('name' => 'isactive', 'display_name' => 'Đang hoạt động'));
		$act = $this->input->get('act', true);
		if(!empty($act)){
			if($act == "view"){
				$orderid = $this->input->get('orderid', true);
				$this->detail(1,'isactive');
				if($this->input->post('btnCancelDDH')){
					$this->cancel($orderid, 'isactive');	
				}
			}
		}else{
			$this->manager(1,'isactive');
		}
	}
	
	//Quan ly don dat hang cho kiem duyet
	public function expire(){
		$this->headTitle('Đơn đặt hàng hết hạn - Admin Panel');
		$this->loadData('action',array('name' => 'expire', 'display_name' => 'Hết hạn'));
		$act = $this->input->get('act', true);
		if(!empty($act) && $act == "view"){
			$this->detail(2,'expire');
		}else{
			$this->manager(2,'expire');
		}
	}
	
	
	public function manager($status, $actionname){
		$per_page = 20;
		$num_link = 7;
		$this->load->helper('form');
		$this->load->library('form_validation');

		$currentPage = $this->input->get('page',true);
		$currentPage = (!empty($currentPage))?$currentPage:'1';
		$this->load->model('Orders_Model');
		$countorders = $this->Orders_Model->countOrders($status);
		$this->loadData('countorders',$countorders);
		$pagingConfig = $this->init_pagination('administrator/order/'.$actionname.'?act=paging', $countorders, $per_page, $num_link);
		$this->loadData('paging', $this->pagination->create_links());
		$this->loadData('listorders', $this->Orders_Model->getOrdersLimit($status,(($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));
		
		/*$spid = $this->input->get('spid', true);
		if(!empty($spid)){
			$this->loadData('servicepakid', $spid);
			$pagingConfig = $this->init_pagination('administrator/order/waitcensor?spid='.$spid, $countorders, $per_page, $num_link);
			$this->loadData('paging', $this->pagination->create_links());
			$this->loadData('listorders', $this->Orders_Model->getOrdersLimit(0,(($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));
		}*/
			
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$fieldsearch = trim($this->input->post('field_search', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('paging','');
				$this->loadData('listorders', $this->Orders_Model->searchOrders($keysearch, $fieldsearch, $status));
			}

		}
		$this->loadView('administrator/order/manager');
	
	}
	
	public function detail($status, $actionname){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Orders_Model');
		$orderid = $this->input->get('orderid', true);
		$order = $this->Orders_Model->getOrderByOrderId($orderid, $status);
		if($order){
			$this->loadData('order', $order);
			$this->loadView('administrator/order/detail');
		}else{
			redirect('administrator/order/'.$actionname);
		}
	}
	
	public function cancel($orderid, $actionname){
		$this->load->model('Orders_Model');
		$order = $this->Orders_Model->getOrderByOrderDetail($orderid);
		if($order){
			$subject = "Thông báo hủy hóa đơn dịch vụ của Truyền hình cáp Đồng Tháp";
			$message = "Thông tin hóa đơn số #".$orderid." không hợp lệ.";
			if($this->sendMail($order->Email, $subject, $message)){
				$this->Orders_Model->deleteOrder($orderid);				
			}
		}
		redirect('administrator/order/'.$actionname);	
	}
	
}